package com.wtgroup.demo.mianshi.算法.字符串.排序;

import java.util.Arrays;

/**低位优先排序
 * @author dafei
 * @version 0.1
 * @date 2021/2/4 15:30
 */
public class LowerFirstSort {
    /*
     * 1. 频次统计; 2. 频次转索引; 3. 排序.
     * 每个位做同样的操作.
     *
     * 字符串等长.
     *
     * 键索引计数法
     *
     */

    public static void main(String[] args) {
        String arr[] = new String[]{"Abc4D", "5Lhou", "tslLH", "gsLhs", "4sflg"};

        int W = 5;
        int R = 256;

        for (int d = W-1; d>=0; d--) {
            int[] count = new int[R + 1];
            for (int j = 0; j < arr.length; j++) {
                count[arr[j].charAt(d) + 1]++;
            }
            for (int r = 0; r < R; r++) {
                count[r + 1] += count[r];
            }
            String[] aux = new String[arr.length];
            for (int j = 0; j < arr.length; j++) {
                aux[count[arr[j].charAt(d)]++] = arr[j];
            }

            for (int i = 0; i < arr.length; i++) {
                arr[i] = aux[i];
            }
        }

        System.out.println(Arrays.toString(arr));

    }

}
